#include <amxmodx>
#include <fun>
#include <rpgx>

#define HEAL_INTERVAL 2.0

new upgrade_id;

public plugin_init()
	register_plugin("Medic RPGx Upgrade", "2.0", "roflmao456");

public RPGx_OnPluginReady(){
	upgrade_id = RPGx_MakeUpgrade("Medic", 5, 15, 20);
	set_task(HEAL_INTERVAL, "loop_medic", 0, "", 0, "b");
	}

public loop_medic(){
	new players[32], num, player;
	new level, health, bonus, xplayer, origin[3], torigin[3];
	get_players(players, num, "a");
	
	for(new i;i<num;i++){
		player = players[i];
		level = RPGx_GetUpgradeLevel(player, upgrade_id);
		if(level){
			get_user_origin(player, origin);
			for(new x;x<num;x++){
				xplayer = players[x];
				if(player != xplayer && get_user_team(xplayer) == get_user_team(player)){
					get_user_origin(xplayer, torigin);
					if(get_distance(origin, torigin) <= 120){
						health = get_user_health(xplayer);
						bonus = RPGx_GetSpawnHealth(xplayer);
						set_user_health(xplayer, bonus-health<=level?health+(bonus-health):health+level);
						}
					}
				}
			}
		}
	}